<?php

/**
 * Created by PhpStorm.
 * User: shwy
 * Date: 2017/5/31
 * Time: 14:01
 * 审核管理
 */
class Check extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('check_model');
        $this->load->model('purchaseOrder_model');

    }

    //审核首页
    public function index()
    {

        $page_num = '4';//每页的数据
        $start_position = ''; //初始数据
        $page = ($this->input->get('page')); //获取页码数量

        if (!$page) {
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        } else {
            $start_position = ($page - 1) * $page_num;
        }


        $status = (isset($_GET['status'])) ? trim($_GET['status']) : 123;

//        print_r($status);
//        die();
//

        //判断搜索内容是否存在存在就继续
        if($this->input->get('search')){
            $searchArg = [
                'field' => 'name',
                'arg'=> $this->input->get('search'),
                'side' => 'both'
            ];

            if($status == '123'){
                $where = 0;
            }elseif($status == 'w'){
                $where = [
                    'status =' => 0
                ];
            }else{
                $where = [
                    'status !=' =>0
                ];
            }

//            print_r($where);
//            die();
            $a = 1;
            if($a){
                $data = $this->check_model->search('*',$where,$searchArg,$page_num,$start_position);
                $viewData = [
                    'list' => $data[0],  //这里得到从数据库中的当页数据
                    'totalNum' => count($data['total_num']), //这里得到从数据库中的总页数
                    'pageNum'  => $page_num //每页显示多少条
                ];
                $this->load->view('admin/check/list',$viewData);
            }else{
                $data = $this->purchaseOrder_model->search('*',$where,$searchArg,$page_num,$start_position);
                $viewData = [
                    'purlist' => $data[0],  //这里得到从数据库中的当页数据
                    'totalNum' => count($data['total_num']), //这里得到从数据库中的总页数
                    'pageNum'  => $page_num //每页显示多少条
                ];
                $this->load->view('admin/procurement/purchaseOrder/list', $viewData);
            }

        } else {


            if($status == '123'){
                $where = 0;
            }elseif($status == 'w'){
                $where = [
                    'status =' => 0
                ];
            }else{
                $where = [
                    'status !=' =>0
                ];
            }

            $a = 0;
            if($a){
                $data = $this->check_model->page('check',$where,$page_num,$start_position); //limit查询的数据

                if($where){
                    $viewData = [
                        'list' => $data[0],  //这里得到从数据库中的当页数据
                        'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                        'pageNum'  => $page_num //每页显示多少条

                    ];
                }else{
                    $viewData = [
                        'list' => $data[0],  //这里得到从数据库中的当页数据
                        'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                        'pageNum'  => $page_num //每页显示多少条

                    ];
                }
                $this->load->view('admin/check/list',$viewData);

            }else{
                $data = $this->purchaseOrder_model->page('purchaseOrder',$where,$page_num,$start_position); //limit查询的数据

                if($where){
                    $viewData = [
                        'purlist' => $data[0],  //这里得到从数据库中的当页数据
                        'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                        'pageNum'  => $page_num //每页显示多少条

                    ];
                }else{
                    $viewData = [
                        'purlist' => $data[0],  //这里得到从数据库中的当页数据
                        'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                        'pageNum'  => $page_num //每页显示多少条

                    ];
                }

//                print_r($viewData);

                $this->load->view('admin/check/list', $viewData);

            }


            //  调试所有sql数据
            //  $this->output->enable_profiler(TRUE);

        }

    }

    /**
     * 审核编辑
     */
    public function edit(){
//        print_r($_GET);
//        die();
        //用户id
        $where = [
            'id' => $this->input->get('id')
        ];
        //todo 更多字段
        $data = [
            'status' => $this->input->get('status'),
//            'status' => 1
        ];

        //是否会有添加失败的情况?
        $resultData =  $this->check_model->edit($data,$where);

        if($resultData){
            echo '<script>alert("修改成功")</script>';
            redirect('/admin/check/index');
        }




    }


    public function detail(){
        $this->load->view('/admin/check/detail');
    }




















}